{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "tensorflow 2 ,加载训练好的权重\n",
    "\n",
    "修改权重\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "#from tensorflow import keras"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "minst = tf.keras.datasets.mnist\n",
    "img_rows,img_cols = 28,28\n",
    "(x_train, y_train), (x_test, y_test) = minst.load_data()\n",
    "x_train = x_train.reshape(x_train.shape[0],img_rows,img_cols,1)\n",
    "x_test = x_test.reshape(x_test.shape[0],img_rows,img_cols,1)\n",
    "\n",
    "x_train = x_train.astype('float32')\n",
    "x_test = x_test.astype('float32')\n",
    "x_train = x_train / 255\n",
    "x_test = x_test / 255\n",
    "y_train_onehot = tf.keras.utils.to_categorical(y_train)\n",
    "y_test_onehot = tf.keras.utils.to_categorical(y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test accuracy: 0.98500\n"
     ]
    }
   ],
   "source": [
    "model = tf.keras.Sequential()\n",
    "model = tf.keras.models.load_model('models/mnist_tf2_fw.h5')\n",
    "score = model.evaluate(x_test, y_test_onehot, verbose=0)\n",
    "#print('Test loss:', score[0])\n",
    "print('Test accuracy:', \"{:.5f}\".format(score[1]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 修改权重，增加各层噪声\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test accuracy: 0.53270\n"
     ]
    }
   ],
   "source": [
    "model = tf.keras.models.load_model('models/mnist_tf2_fw.h5')\n",
    "for i in  [0,2,5]:\n",
    "    temp = model.layers[i].get_weights()[0]\n",
    "    temp = temp + np.random.rand(*temp.shape)*0.16\n",
    "    model.layers[i].set_weights([temp])\n",
    "'''    \n",
    "conv2_1w = model.layers[0].get_weights()[0]\n",
    "conv2_1b = model.layers[0].get_weights()[1]\n",
    "conv2_1w_2 = conv2_1w + np.random.rand(3,3,1,8)\n",
    "model.layers[0].set_weights([conv2_1w_2,conv2_1b])\n",
    "'''\n",
    "score = model.evaluate(x_test, y_test_onehot, verbose=0)\n",
    "print('Test accuracy:', \"{:.5f}\".format(score[1]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 16bit量化权重"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test accuracy: 0.66250\n"
     ]
    }
   ],
   "source": [
    "bit = 1\n",
    "model = tf.keras.models.load_model('models/mnist_tf2_fw.h5')\n",
    "for i in  [0,2,5]:\n",
    "    temp = model.layers[i].get_weights()[0]\n",
    "    temp = np.rint(temp * np.power(2,bit))/np.power(2,bit)\n",
    "    model.layers[i].set_weights([temp])\n",
    "score = model.evaluate(x_test, y_test_onehot, verbose=0)\n",
    "print('Test accuracy:', \"{:.5f}\".format(score[1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.125, -0.5  , -0.25 , ..., -0.125,  0.125,  0.125],\n",
       "       [-0.125,  0.   , -0.125, ..., -0.   ,  0.   ,  0.25 ],\n",
       "       [ 0.375, -0.   , -0.125, ...,  0.25 , -0.25 , -0.   ],\n",
       "       ...,\n",
       "       [-0.375,  0.125,  0.125, ..., -0.125, -0.125,  0.25 ],\n",
       "       [-0.375,  0.375,  0.125, ...,  0.   , -0.125,  0.125],\n",
       "       [-0.5  ,  0.625,  0.25 , ...,  0.   , -0.625,  0.125]],\n",
       "      dtype=float32)"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "temp"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "oldHeight": 392.183666,
   "position": {
    "height": "40px",
    "left": "607px",
    "right": "20px",
    "top": "17px",
    "width": "575.333px"
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "varInspector_section_display": "none",
   "window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
